# 异常处理
# Python
try:
    pass
except Exception as e:
    pass
else:
    pass
finally:
    pass
# Java
# try{
#
# }catch Exception {
#
# }finally{
#
# }

try:
    # 可能会出现异常的代码
    # "abc" + 1
    10 / 1
except ZeroDivisionError as e:
    print("捕获到了除零异常才会执行")
    print("请检查除数")
    print(e)
except ValueError as e:
    print("发生了ValueError")
except Exception as e:
    print("可以捕获到任何错误")
else:
    print("没有发生异常，就执行else语句")
finally:
    print("不管有没有发生异常，都会执行")


# 自定义异常类
class MyError(Exception):
    pass


def func(age):
    if age < 0:
        raise MyError
    if age >= 18:
        return "adult"
    elif age < 18:
        return "no adult"


try:
    raise MyError
except MyError as e:
    print("捕获到了自定义异常")
    print(e)

try:
    print(func(18))
    print(func(16))
    print(func(-1))
except MyError as e:
    print("捕获到了MyError")


